package com.shiguiwu.springmybatis.designpattern.adapter.scene.service;

import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.starter.annotation.LogRecord;
import com.shiguiwu.springmybatis.app.entity.LogRecordType;
import com.shiguiwu.springmybatis.app.entity.Order;
import com.shiguiwu.springmybatis.app.repository.LogRecordRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description: 订单服务
 * @author: stone
 * @date: Created by 2021/8/19 11:01
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.designpattern.adapter.scene.service
 */
@Slf4j
@Service
public class OrderService {


    @Resource
    private LogRecordRepository logRecordRepository;
    public long queryUserOrderCount(String userId) {
        log.info("自营商家，查询用户的订单是否为首单：{}", userId);
        return 10L;
    }


    @LogRecord(
            fail = "创建订单失败，失败原因：「{{#_errorMsg}}」",
            subType = "MANAGER_VIEW",
            extra = "{{#order.toString()}}",
            success = "{{#order.purchaseName}}下了一个订单,购买商品「{{#order.productName}}」,测试变量「{{#innerOrder.productName}}」,下单结果:{{#_ret}}",
            type = LogRecordType.ORDER, bizNo = "{{#order.orderNo}}")
    public boolean createOrder(Order order) {

        log.info("【创建订单】orderNo={}", order.getOrderNo());
        // db insert order
        Order order1 = new Order();
        order1.setProductName("内部变量测试");
        LogRecordContext.putVariable("innerOrder", order1);
        return true;
    }


    @LogRecord(
            fail = "创建订单失败，失败原因：「{{#_errorMsg}}」",
            subType = "MANAGER_VIEW",
            operator ="{{#currentUser}}" ,
            extra = "{{#order.toString()}}",
            success = "{{#order.purchaseName}}下了一个订单,购买商品「{{#order.productName}}」,测试变量「{{#innerOrder.productName}}」,下单结果:{{#_ret}}",
            type = LogRecordType.ORDER, bizNo = "{{#order.orderNo}}")
    public boolean createOrder(Order order,String currentUser) {

        log.info("【创建订单】orderNo={}", order.getOrderNo());
        // db insert order
        Order order1 = new Order();
        order1.setProductName("内部变量测试");
        LogRecordContext.putVariable("innerOrder", order1);
        return true;
    }




    //使用了自定义函数，主要是在 {{#orderId}} 的大括号中间加了 functionName
    @LogRecord(success = "更新了订单{ORDER{#orderId}},更新内容为...",
            type = LogRecordType.ORDER, bizNo = "{{#order.orderNo}}",
            extra = "{{#order.toString()}}")
    public boolean update(Long orderId, Order order) {
        order.setPurchaseName("尼玛");

        return false;
    }



    @LogRecord(success = "更新了订单{_DIFF{#oldOrder, #newOrder}}",
            type = LogRecordType.ORDER, bizNo = "{{#newOrder.orderNo}}")
    public boolean diff(Order oldOrder, Order newOrder) {

        return false;
    }
}
